frappe.ui.form.on('Sales Invoice', {
	
	setup(frm){
	   // 取物料主数据设置的单位
	    
	  frm.set_query("uom", "items", function (doc, cdt, cdn) {
      let row = locals[cdt][cdn];
      return {
        query:
          "erpnext.accounts.doctype.pricing_rule.pricing_rule.get_item_uoms",
        filters: {
          value: row.item_code,
          apply_on: "Item Code",
        },
      };
    });
    
	},
	
	refresh(frm){
	    
	    //取第一销售订单上的付款条件
	    
	    if(frm.is_new())
	       {
        	    let delivery_note=''
            	for(let i=0;i<frm.doc.items.length;i++)
                {
                    let delivery_note_1=frm.doc.items[i].delivery_note
                    if(delivery_note_1)
                    {
                        delivery_note=delivery_note_1
                       
                        break;
                    }
                }
                
                
                // console.log(delivery_note)
              
                if(delivery_note)
                    {
                        frappe.db.get_value('Delivery Note',{'name':delivery_note},'`tabDelivery Note Item`.against_sales_order').then(
                            (r)=>{
                                console.log(r.message);
                                if(r.message){
                                  $.each(r.message,(key,value)=>{
                                      // console.log(value)
                                      if(value){
                                          console.log(value);
                                          frappe.db.get_value('Sales Order',{'name':value},'payment_terms_template').then(
                                              (r)=>{
                                                //    console.log(r.message.payment_terms_template)
                                                  let payment_terms_template=r.message.payment_terms_template
                                                  frm.set_value("payment_terms_template", payment_terms_template)
                                                  refresh_field('payment_terms_template');
            
            
                                              }
                                          )
            
                                          return false// break;
                                      }
                                  })
                                }
                            }
                        )
                    }
              
                // frappe.call({
                //     method:'get_payment_terms_template',
                //     args:{
                //     delivery_note:delivery_note,
                //     },
                //     callback:function(r){
                //         console.log(r.message)
                //     }
                    
                // })  
                
            	}
	}
	
	
})